const app = getApp();
const _DB = require('../../../utils/db.js')
Page({
  data: {
    tempFilePaths: '',
    studentID:"",
    name:"",
    phone:"",
    grade:"",
    specialities:"",
    address:"",
    assisant:"",
    password:"",
    sysList: ['软件系', '网络系', '计算机系', '数码系', '电子系', '国贸系', '财会系', '游戏系', '管理系', '外语系'],
    sysName: '------选择您的所在系------',
  },

  selectSys: function (e) {
    var index = e.detail.value;
    var _this = this;
    this.setData({ sysName: _this.data.sysList[index] });
  },
  

  bindSave: function (e) {
    const _this = this
    var studentID = e.detail.value.studentID;
    var name = e.detail.value.name;
    var phone = e.detail.value.phone;
    var grade = e.detail.value.grade;
    var specialities = e.detail.value.specialities;
    var address = this.data.sysList[e.detail.value.address];
    var assisant = e.detail.value.assisant;
    var password=e.detail.value.password;

    console.log(address)

    var myreg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(17[0-9]{1}))+\d{8})$/;
    var info = {};

    wx.showToast({
      title: '正在注册',
      icon:'loading',
      mask:true,
    })


    if (studentID == '') {
      wx.showToast({
        title: '请输入学号！',
        icon: 'none',
        duration: 2000,
      })
      return false
    } else if (studentID.length !=10) {
      wx.showToast({
        title: '输入学号有误',
        icon: 'none',
        duration: 2000,
      })
    }
    else if(name == ''){
      wx.showToast({
        title: '请输入姓名',
        icon:'none',
        duration:2000,
      })
      return false
    } else if (grade == '' || grade.length != 4) {
      wx.showToast({
        title: '年级格式错误，如：2017',
        icon: 'none',
        duration: 2000,
      })
      return false
    } else if (password == '') {
      wx.showToast({
        title: '请输入密码',
        icon: 'none',
        duration: 2000,
      })
      return false
    } 
    else if (specialities == '') {
      wx.showToast({
        title: '请输入专业',
        icon: 'none',
        duration: 2000,
      })
      return false
    } else if (address == '') {
      wx.showToast({
        title: '请输入所在系',
        icon: 'none',
        duration: 2000,
      })
      return false
    } else if (assisant == '') {
      wx.showToast({
        title: '请输入辅导员',
        icon: 'none',
        duration: 2000,
      })
      return false
    } else if (phone == '') {
      wx.showToast({
        title: '请输入手机号',
        icon: 'none',
        duration: 2000,
      })
      return false
    } else if (phone.length != 11) {
      wx.showToast({
        title: '手机号长度有误！',
        icon: 'none',
        duration: 2000
      })
      return false
    }else if (!myreg.test(phone)) {
      wx.showToast({
        title: '手机号有误！',
        icon: 'none',
        duration: 1500
      })
      return false;
    } else{
      info.studentID = studentID
      info.name = name
      info.phone = phone
      info.grade = grade
      info.specialities = specialities
      info.address = address
      info.assisant = assisant
      info.password = password

    
      _DB.getInfoByid(studentID,function(res){
        if(res.length == 0){
          //调用mysql方法存入数据库
          _this.mysql(info);
        }else{
          wx.showToast({
            title: '该用户已经注册过了，忘记密码请联系管理员',
            icon:'none'
          })
        }

      })

    }


  },

  mysql: function (info) {
    var that=info;
    app.globalData.studentID = that.studentID;


    // console.log(app.globalData.studentID)
    // console.log(app.globalData.name)
    // console.log(app.globalData.grade)

    wx.showToast({
      title: '正在提交信息',
      icon: 'loading',
      mask: true,
    })

    wx.cloud.callFunction({
      name: "mysql",
      data:{
        studentID: that.studentID,
        name: that.name,
        phone: that.phone,
        grade: that.grade,
        specialities: that.specialities,
        address: that.address,
        assisant: that.assisant,
        password:that.password,
      },
      success(res) {

        wx.showModal({
          title: '提示',
          content: '注册成功，是否跳转登录页面',
          success: function (res) {
            if (res.confirm) {
              wx.navigateBack({})
            } else if (res.cancel) {
              console.log('用户点击取消')
            }
          }
        })
      },
      fail(res) {
        wx.showToast({
          title: '注册失败，请检查信息是否有误！',
          icon: 'none'
        })

      }
    })
  },


})
